package com.dd.admin.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.common.dto.system.user.SysUserQueryDTO;
import com.dd.common.dto.user.UserQueryDTO;
import com.dd.common.entity.SysRole;
import com.dd.common.entity.User;
import com.dd.common.enums.ErrorEnum;
import com.dd.common.exception.BusinessException;
import com.dd.common.pojo.dto.PageDTO;
import com.dd.common.service.UserService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.PageResult;
import com.dd.common.validator.annotation.IDMust;
import com.dd.common.vo.SysUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@SaIgnore
@Validated
@Slf4j
@Tag(name = "用户管理", description = "前台用户管理接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Operation(summary = "用户分页列表", description = "分页查询前台用户信息")
    @GetMapping("/page")
    public AjaxResult<PageResult<User>> page(@Validated PageDTO pageDTO, @Validated UserQueryDTO dto) {
        Page<User> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getNickname()), User::getNickname, dto.getNickname())
                .like(StringUtils.hasText(dto.getRealName()), User::getRealName, dto.getRealName())
                .eq(dto.getGender() != null, User::getGender, dto.getGender())
                .ge(dto.getCreatedAtStart() != null, User::getCreateTime, dto.getCreatedAtStart())
                .le(dto.getCreatedAtEnd() != null, User::getCreateTime, dto.getCreatedAtEnd())
                .ge(dto.getUpdatedAtStart() != null, User::getUpdateTime, dto.getUpdatedAtStart())
                .le(dto.getUpdatedAtEnd() != null, User::getUpdateTime, dto.getUpdatedAtEnd())
                .orderByAsc(User::getId);
        IPage<User> result = userService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

    @Operation(summary = "用户状态", description = "用户状态切换", parameters = @Parameter(name = "id", description = "用户ID", in = ParameterIn.PATH))
    @PatchMapping("/status/{id}")
    public AjaxResult<Boolean> status(@PathVariable @IDMust Long id) {
        User user = userService.getById(id);
        if (user == null) {
            throw new BusinessException(ErrorEnum.USER_NOT_FOUND);
        }
        user.setStatus(user.getStatus() == 1 ? 0 : 1);
        return AjaxResult.success(userService.updateById(user));
    }
}
